package com.example.mobile2021606145zbq0617homework;

import android.content.ContentValues;
import android.content.Intent;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

import androidx.appcompat.app.AppCompatActivity;

import java.net.Inet4Address;

public class MainActivity2 extends AppCompatActivity {

    private TextView tvMainTitle2;
    private EditText edtMainName;
    private EditText edtMainVocation;
    private EditText edtMainSalary;
    private Button btnMainCancel;
    private Button btnMainConfirm;
    MySQLiteOpenHelper openHelper;
    SQLiteDatabase database;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main2);
        initView();
        openHelper = new MySQLiteOpenHelper(this,"factory.db",null,1);
        btnMainCancel.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });
        btnMainConfirm.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                String name = edtMainName.getText().toString().trim();
                String vocation = edtMainVocation.getText().toString().trim();
                int salary = Integer.valueOf(edtMainSalary.getText().toString().trim());
                ContentValues contentValues = new ContentValues();
                contentValues.put("name", name);
                contentValues.put("vocation", vocation);
                contentValues.put("salary",salary);
                database=openHelper.getWritableDatabase();
                database.insert("staff",null,contentValues);
                database.close();
                Intent intent = new Intent(MainActivity2.this,MainActivity.class);
                startActivity(intent);
            }
        });
    }

    private void initView() {
        tvMainTitle2 = (TextView) findViewById(R.id.tvMainTitle2);
        edtMainName = (EditText) findViewById(R.id.edtMainName);
        edtMainVocation = (EditText) findViewById(R.id.edtMainVocation);
        edtMainSalary = (EditText) findViewById(R.id.edtMainSalary);
        btnMainCancel = (Button) findViewById(R.id.btnMainCancel);
        btnMainConfirm = (Button) findViewById(R.id.btnMainConfirm);
    }
}